<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function _initialize()
    {

        Vendor('Elasticsearch.autoload');
        $params['hosts'] = array(
            'http://localhost:9200'
        );
        $this->client = new \Elasticsearch\Client($params);
    }
    public function index(){
        $this->search();
    }
    
  //创建索引
  //现在我们开始添加一个新的索引和一些自定义设置：
    public function create_index()
    {

        $indexParams['id'] = 2;
        $indexParams['index'] = 'myindextest';                       //索引名称
        $indexParams['type'] = 'mytype';                        //类型名称
        $indexParams['body']['settings']['number_of_shards'] = 1; //当前只有一台ES，1就可以了
        $indexParams['body']['settings']['number_of_replicas'] = 0;//副本0，因为只有一台ES
        $this->client->create($indexParams);
    }
  //插入索引数据
    public function add_document()
    {

       ini_set('memory_limit','3072M');  // 临时设置最大内存占用为3G
       set_time_limit(0); // 设置脚本最大执行时间 为0 永不过期
    
       for ($i=152268; $i <10000000 ; $i++) { 
           # code.
            $params = array();
            $params['body'] = array(
                'id' => $i,
                'name' => "父亲".$i,
                "age"=>rand(1,9)*rand(1,9),
                'shgx'=>array(
                    'id'=> $i+1,
                    'name'=>'儿子'. $i,
                    'age'=>rand(1,9)*rand(1,9),
                    'gx'=>'第'
                )
            );
            $params['index'] = 'test_sort';//索引名称
            $params['type'] = 'zf'; //类型名称
            $params['id'] =  $i;   //不指定id，系统会自动生成唯一id

     



           $ret = $this->client->index($params);
       }
       
    }
  //删除索引
  //由于 elasticsearch 的动态性质，我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引，因为我们以后想要指定自己的设置：
    public function delete_index()
    {
        $deleteParams['index'] = 'myindextest';
        $this->client->indices()->delete($deleteParams);
    }
  //删除文档
    public function delete_document()
    {
        $deleteParams = array();
        $deleteParams['index'] = 'myindextest';
        $deleteParams['type'] = 'mytype';
        $deleteParams['id'] = '123';
        $retDelete = $this->client->delete($deleteParams);
    }
  //更改文档
    public function update_document()
    {
        $updateParams = array();
        $updateParams['index'] = 'myindextest';
        $updateParams['type'] = 'mytype';
        $updateParams['id'] = 'my_id';
        $updateParams['body']['doc']['product_name']  = '新商品名';
       $response = $this->client->update($updateParams);
         
    }
  //查询
    public function search()
    {
        $searchParams['index'] = 'test_sort';
        $searchParams['type'] = 'zf';
        $searchParams['from'] = 0;
        $searchParams['size'] = 100;
      $searchParams['sort'] = array(
            '_id' => array(
                'order' => 'desc'
            )
        );
       $searchParams['body']['sort'] = array(  
                array('id' => array('order' => 'desc')),  
              );



    
       
      //p($searchParams);

      //相当于sql语句：  select * from hp_product where prodcut_name like '茶'  limit 0,100 order by id desc;
      //$searchParams['body']['query']['match']['product_name'] = '商品';
       
      //相当于sql语句： select * from hp_product where product_name like '茶'  and product_id = 20 limit 200,10;
         $searchParams['body']['query']['bool']['should'] = array(  
             array('match' => array('name' => '152267')),  
           //array('match' => array('id' => 1))
            );  
      // $searchParams['size'] = 10;
      // $searchParams['from'] = 200;
      //
      //
      // 当于sql语句：select * from hp_product where product_name like '茶' or product_id = 20 limit 200,10;
         
       $searchParams['body']['query']['bool']['should'] = array(
                array('match' => array('product_name' => '测试')),  
                array('match' => array('product_id' => 5))  
              );
      //$searchParams['size'] = 10;
      //$searchParams['from'] = 200;
      //
      //
      // 当于sql语句： select * from hp_product where product_name like '茶' and product_id != 20 limit 200,10;
      // $searchParams['body']['query']['bool']['must_not'] = array(
      //        array('match' => array('product_name' => '茶')),
      //        array('match' => array('product_id' => 20))
      //      );
      //$searchParams['size'] = 10;
      //$searchParams['from'] = 200;
      //
      //
      //当于sql语句：select * from hp_product where id>=20 and id<30  limit 200,10;
      // $searchParams['body']['query']['range'] = array(
      //        'id' => array('gte' => 20,'lt' => 30);
      //      );
      //$searchParams['size'] = 10;
      //$searchParams['from'] = 200;
         $retDoc = $this->client->search($searchParams);
        
        echo '<pre>';
        print_r($retDoc['hits']);
    }
  //获取文档
    public function get_document()
    {
        $getParams = array();
        $getParams['index'] = 'myindextest';
        $getParams['type'] = 'mytype';
        $getParams['id'] = '12344';
        $retDoc = $this->client->get($getParams);
        print_r($retDoc);
    } 
}
?>
